/*
 * CLI.cpp
 *
 *  Created on: Mar 24, 2011
 *      Author: kimi
 */

#include "CLI.h"
#include <iostream>
#include <string.h>
#include "CLIEntity.h"
#include <getopt.h>
#include <unistd.h>
#include <ctype.h>

CLI::CLI() {

}

CLI::~CLI() {

}

CLIEntity* CLI::processCommand(int argc, char* argv[]) {

	CLIEntity *entity = new CLIEntity();

	int opt;

	entity->command = CLIEntity::CLI_INVALID_COMMAND;
	entity->subCommand = CLIEntity::CLI_INVALID_SUBCOMMAND;
	entity->fileName = NULL;
	entity->recordId = NULL;
	entity->user = NULL;
	entity->password = NULL;

	if (argc == 1) {
		cerr << "El programa necesita argumentos..." << endl;
		return entity;
	}

/*
 * 	enum CLI_COMMAND {
		CLI_INVALID_COMMAND = '0',
		CLI_DISTRITOS_COMMAND = 'd',
		CLI_VOTANTES_COMMAND = 'v',
		CLI_ELECCIONES_COMMAND = 'e',
		CLI_LISTAS_COMMAND = 'l',
		CLI_CARGOS_COMMAND = 'c',
		CLI_CANDIDATOS_COMMAND = 't',
		CLI_VOTAR_COMMAND = 'a'
	};

	enum CLI_SUBCOMMAND {
		CLI_INVALID_SUBCOMMAND = '0',
		CLI_INSERTAR_SUBCOMMAND = 'i',
		CLI_VER_SUBCOMMAND = 'x',
		CLI_BORRAR_SUBCOMMAND = 'b'
	};
 *
 */

	while ((opt = getopt(argc, argv, "fdvelcwtazi:x:b:r:1:2:3:y:u:p:")) != -1) {
		switch (opt) {
			case 'd': {entity->command = CLIEntity::CLI_DISTRITOS_COMMAND;} break;
			case 'z': {entity->command = CLIEntity::CLI_ENCRIPTAR_ARCHIVO_ADMIN_COMMAND; entity->subCommand = CLIEntity::CLI_NO_SUBCOMMAND;} break;
			case 'w': {entity->command = CLIEntity::CLI_ROMPER_RSA; entity->subCommand = CLIEntity::CLI_NO_SUBCOMMAND;} break;
			case 'v': {entity->command = CLIEntity::CLI_VOTANTES_COMMAND;} break;
			case 'f': {entity->command = CLIEntity::CLI_COMPROBAR_INTEGRIDAD; entity->subCommand = CLIEntity::CLI_NO_SUBCOMMAND;} break;
			case 'e': {entity->command = CLIEntity::CLI_ELECCIONES_COMMAND;} break;
			case 'l': {entity->command = CLIEntity::CLI_LISTAS_COMMAND;} break;
			case 'c': {entity->command = CLIEntity::CLI_CARGOS_COMMAND;} break;
			case 't': {entity->command = CLIEntity::CLI_CANDIDATOS_COMMAND;} break;
			case 'a': {entity->command = CLIEntity::CLI_VOTAR_COMMAND; entity->subCommand = CLIEntity::CLI_NO_SUBCOMMAND;} break;
			case 'r': {entity->command = CLIEntity::CLI_REPORTE_COMMAND; entity->fileName = optarg;} break;

			case 'i': {entity->subCommand = CLIEntity::CLI_INSERTAR_SUBCOMMAND; entity->fileName = optarg;} break;
			case 'x': {entity->subCommand = CLIEntity::CLI_VER_SUBCOMMAND; entity->recordId = optarg;} break;
			case 'b': {entity->subCommand = CLIEntity::CLI_BORRAR_SUBCOMMAND;entity->recordId = optarg;} break;

			case '1': {entity->subCommand = CLIEntity::CLI_REPORTE_ELECCION_SUBCOMMAND; entity->recordId = optarg;} break;
			case '2': {entity->subCommand = CLIEntity::CLI_REPORTE_DISTRITO_SUBCOMMAND; entity->recordId = optarg;} break;
			case '3': {entity->subCommand = CLIEntity::CLI_REPORTE_LISTA_SUBCOMMAND; entity->recordId = optarg;} break;

			case 'y': {entity->command = CLIEntity::CLI_VOTO_AUTOMATICO_COMMAND;
				entity->subCommand = CLIEntity::CLI_NO_SUBCOMMAND; entity->fileName = optarg;} break;

			case 'u': {entity->user = optarg;} break;
			case 'p': {entity->password = optarg;} break;
		}
	}

	return entity;
}

bool CLI::entityRequiresLogin(CLIEntity *entity) {
	if (entity->command == CLIEntity::CLI_VOTAR_COMMAND) {
		return false;
	}
	if (entity->command == CLIEntity::CLI_VOTO_AUTOMATICO_COMMAND) {
		return false;
	}
	if (entity->command == CLIEntity::CLI_ENCRIPTAR_ARCHIVO_ADMIN_COMMAND) {
		return false;
	}
	if (entity->command == CLIEntity::CLI_ROMPER_RSA) {
			return false;
		}
	return true;
}

void CLI::print_help() {

}
